package com.why.serviceedu.controller.front;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.why.commonresults.CourseOrder;
import com.why.commonresults.Result;
import com.why.serviceedu.bean.EduCourse;
import com.why.serviceedu.bean.FrontVo.CourseQueryVo;
import com.why.serviceedu.bean.FrontVo.CourseWebVo;
import com.why.serviceedu.bean.Vo.courseInfoVo;
import com.why.serviceedu.bean.chapter.ChapterVo;
import com.why.serviceedu.client.OrderClient;
import com.why.serviceedu.service.EduChapterService;
import com.why.serviceedu.service.EduCourseService;
import com.why.utils.JwtUtils;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @program: guli_parent
 * @description:
 * @author: @why
 * @create: 2021-02-21 11:31
 **/
@RestController
@CrossOrigin
@RequestMapping("/eduservice")
public class FrontCourseController {

    @Autowired
    EduCourseService courseService;

    @Autowired
    private EduChapterService chapterService;

    @Autowired
    private OrderClient orderClient;

    @ApiOperation(value = "分页课程列表")
    @PostMapping("/courseInfo/{page}/{limit}")
    public Result pageList(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit,
            @ApiParam(name = "courseQuery", value = "查询对象", required = false)
            @RequestBody(required = false) CourseQueryVo courseQuery) {
        Page<EduCourse> pageParam = new Page<EduCourse>(page, limit);
        Map<String, Object> map = courseService.pageListWeb(pageParam, courseQuery);
        return Result.ok().data(map);
    }

    /**
     * 课程详情页面查询课程的详情
     *
     * @param courseId
     * @return
     */

    @ApiOperation(value = "根据ID查询课程")
    @GetMapping(value = "/courseDetail/{courseId}")
    public Result getById(
            @ApiParam(name = "courseId", value = "课程ID", required = true)
            @PathVariable String courseId, HttpServletRequest request) {
        System.out.println("edu中得到token"+request.getHeader("token"));
//根据课程id查询课程信息和讲师信息
        CourseWebVo courseWebVo = courseService.selectInfoWebById(courseId);
//根据课程id查询当前课程的章节信息,Video的vo类记得加入视频源id
        List<ChapterVo> chapterVoList = chapterService.getAllChapterVideoByCourseId(courseId);
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
//根据用户id以及课程id查询是否有订单且订单已经支付了

        //memberId="1361641639986126850";
        boolean isBuy = orderClient.isBuyCourse(memberId, courseId);


        System.out.println(isBuy);
        System.out.println("当前用户的id为----》" + memberId);
        return Result.ok().data("course", courseWebVo).data("chapterVoList",
                chapterVoList).data("isBuy", isBuy);
    }


    //根据课程id查询课程信息
    @GetMapping("/getCourseByCourseId/{courseId}")
    public CourseOrder getCourseInfoDto(@PathVariable String courseId) {
        System.out.println("courseId" + courseId);
        CourseWebVo course = courseService.selectInfoWebById(courseId);
        CourseOrder courseOrder = new CourseOrder();
        System.out.println("远程调用。。。。");
        System.out.println("查询到的courseOrder为" + course);
        BeanUtils.copyProperties(course, courseOrder);
        return courseOrder;
    }


}
